const CommonApi = require('../../../api/common.js');
const UserApi = require('../../../api/user.js');
const regExpUtil = require('../../utils/regExp.js');
const commonUtil = require('../../../utils/common.js');

Page({
  data: {
    tickTip: '获取验证码',
    dialogVisible: false,
    // formModel--start
    phoneNumber: null,
    code: null,
    captcha: null,
    captchaId: null,
    // formModel--end
    // pswModel--start
    loginPassword: null,
    confirmPassword: null,
    // pswModel--end
    redirect_uri: '/pages/index/index',
    redirect_uri_type: 'tab',
    step: 1,
    ActionType: "",
    resetInfo: null,
    timers: 60,
    countDown: null,
  },

  onLoad(options) {
    if (options.redirect_uri) {
      this.setData({redirect_uri: options.redirect_uri})
    }
    if (options.redirect_uri_type) {
      this.setData({redirect_uri_type: options.redirect_uri_type})
    }
    let resetInfo = wx.getStorageSync('resetInfo');
    if (resetInfo) {
      this.setData({ActionType: resetInfo.action, resetInfo: resetInfo})
      let _title = resetInfo.action == 'reset' ? '忘记密码' : '设置密码';
      wx.setNavigationBarTitle({title: _title});
    }
  },
  onReady() {},
  onShow() {},
  onHide() {},

  initData(){
    this.setData({
      phoneNumber: null,
      code: null,
      captcha: null,
      captchaId: null,
      loginPassword: null,
      confirmPassword: null,
    })
  },
  handleInput(e){
    let target = e.currentTarget.dataset.item;
    let value = e.detail.value;
    if (target == 'phoneNumber') {
      this.setData({phoneNumber: value})
    } else if (target == 'code') {
      this.setData({code: value})
    }
  },
  startInterval(){
    let that = this;
    let countDownNum = that.data.timers;

    that.data.countDown = setInterval(()=>{
      if (countDownNum < 1) {
        that.setData({tickTip: '获取验证码', timers: 60});
        clearInterval(that.data.countDown);
      } else {
        countDownNum--;
        that.setData({
          timers: countDownNum,
          tickTip: countDownNum+'s后重发'
        })
      }
    }, 1000)
  },
  getCode(){
    if (this.data.tickTip != '获取验证码') return;
    if (!this.data.phoneNumber) {
      wx.showToast({icon: 'none', title: '请输入手机号码'})
    } else if (this.data.phoneNumber && !regExpUtil.isPhoneNumber(this.data.phoneNumber)) {
      wx.showToast({icon: 'none', title: '请输入正确的手机号码'})
    } else {
      this.getPhoneNumberRegistered();
    }
  },
  // 获取图形验证码弹窗传递来的数据
  getCaptchaData(e){
    console.log(e);
    this.setData({
      dialogVisible: false, 
      captcha: e.detail.captcha,
      captchaId: e.detail.captchaId
    })
    this.sendSms()
  },
  // 判断手机号是否注册，已注册才发送短信验证码
  getPhoneNumberRegistered(){
    UserApi.getPhoneNumberRegistered({phoneNumber: this.data.phoneNumber}).then(res=>{
      if (res.retCode == "000000" && res.errCode == "000000") {
          if (res.data) {
            this.setData({dialogVisible: true})
          } else {
            wx.showToast({icon: 'none', title: '手机号码未注册'})
            return false;
          }
      }
    }).catch(err=>{
        console.log(err)
    })
  },
  // 发送短信验证码
  sendSms(){
    CommonApi.sendSms({phoneNumber: this.data.phoneNumber, code: this.data.captcha, captchaId: this.data.captchaId}).then(res=>{
      if (res.retCode == "000000" && res.errCode == "000000") {
        this.setData({msgSend: true});
        wx.showToast({icon: 'success', title: '短信验证码发送成功'});
        this.startInterval()
      }
    })
  },
  // 校验短信验证码
  checkSms(){
    CommonApi.checkSms({phoneNumber: this.data.phoneNumber, code: this.data.code}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        if (!res.data) {
          wx.showToast({icon: 'error', title: '验证码错误'});
        } else {
          console.log(this.data.resetInfo);
          let _title = this.data.resetInfo.action == 'reset' ? '重置密码' : '设置密码';
          wx.setNavigationBarTitle({title: _title})
          this.setData({step: 2})
        }
      }
    })
  },
  // 未激活-设置密码
  setPassword(){
    let pswModel = {loginPassword: this.data.loginPassword, confirmPassword: this.data.confirmPassword};
    UserApi.setPassword({...pswModel}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        wx.showToast({icon: 'success', title: res.errMsg})
        if (this.data.redirect_uri_type == 'tab') {
          wx.switchTab({url: this.data.redirect_uri})
        } else if (this.data.redirect_uri_type == 'link') {
          wx.redirectTo({url: this.data.redirect_uri})
        }
      }
    })
  },
  // 忘记密码-重置密码
  resetPassword(){
    let formModel = {phoneNumber: this.data.phoneNumber, captcha: this.data.captcha, code: this.data.code};
    let pswModel = {loginPassword: this.data.loginPassword, confirmPassword: this.data.confirmPassword};
    UserApi.resetPassword({...formModel, ...pswModel}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        wx.showToast({icon: 'success', title: res.errMsg})
        this.initData()
        commonUtil.logout()
        if (this.data.redirect_uri_type == 'tab') {
          wx.switchTab({url: this.data.redirect_uri})
        } else if (this.data.redirect_uri_type == 'link') {
          wx.redirectTo({url: this.data.redirect_uri})
        }
      } else {
        wx.showToast({icon: 'error', title: res.errMsg});
      }
    })
  },
  goNext(){
    if (!this.data.phoneNumber) {
      wx.showToast({icon: 'none', title: '请输入手机号码'})
    } else if (this.data.phoneNumber && !regExpUtil.isPhoneNumber(this.data.phoneNumber)) {
      wx.showToast({icon: 'none', title: '请输入正确的手机号码'})
    } else if (!this.data.code) {
      wx.showToast({icon: 'none', title: '请输入验证码'})
    } else {
      this.checkSms()
    }
  },
  submitForm(){
    if (!this.data.loginPassword) {
      wx.showToast({icon: 'none', title: '请输入密码'})  
    } else if (this.data.loginPassword && !regExpUtil.isLegalPsw(this.data.loginPassword)) {
      wx.showToast({icon: 'none', title: '密码必须包含6-16位字母数字'})
    } else if (!this.data.confirmPassword) {
      wx.showToast({icon: 'none', title: '请输入确认密码'})  
    } else if (this.data.confirmPassword != this.data.loginPassword) {
      wx.showToast({icon: 'none', title: '两次输入密码不一致'})  
    } else {
      if (this.data.ActionType == 'reset') {
        this.resetPassword() 
      } else {
        this.setPassword()
      }
    }
  },

  onUnload() {},
})